package com.cbh.amadeus.client.domain;

import com.cbh.amadeus.client.utils.ChannelReference;
import com.cbh.amadeus.common.enums.MessageResult;
import com.cbh.amadeus.common.protocol.AmadeusMessage;
import io.netty.channel.Channel;
import lombok.Data;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author ChenBiHui
 * @date 2025/5/13
 */
@Data
public class MessageMatchModel {

    private final Channel channel;
    private final String name;
    private final AmadeusMessage message;
    private final AtomicInteger cumulative;
    private final Set<String> mount = ConcurrentHashMap.newKeySet(18);

    public MessageMatchModel(Channel channel, String name, AmadeusMessage message, Integer holders) {
        this.channel = channel;
        this.name = name;
        this.message = message;
        this.cumulative = new AtomicInteger(holders);
    }

    public void mount(MessageResult result, String label) {
        if (result == MessageResult.SUCCESS && mount.add(label)) {
            if (cumulative.decrementAndGet() == 0) {
                ChannelReference.ack(channel, message, name);
            }
        }
    }
}
